package com.gc.repository.impl;

import com.gc.controller.studentServlet.UpdateStuStage;
import com.gc.entity.StuCheck;
import com.gc.repository.StuCheckRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class StuCheckRepositoryImpl implements StuCheckRepository {

    @Override
    public boolean Add(StuCheck stuCheck) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into stucheck(sid, checkid, stage) VALUES (?, ?, ?)",
                    stuCheck.getSid(),
                    stuCheck.getCheckid(),
                    stuCheck.getStage()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<StuCheck> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<StuCheck> query = qr.query("select * from stucheck", new BeanListHandler<>(StuCheck.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from stucheck WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public StuCheck FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            StuCheck res = qr.query("select * from stucheck WHERE id = ?", new BeanHandler<>(StuCheck.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<StuCheck> FindByStudentId(String sid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<StuCheck> query = qr.query("select * from stucheck WHERE sid = ?", new BeanListHandler<>(StuCheck.class), sid);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(StuCheck stuCheck) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update stucheck set sid = ? , checkid = ? , stage = ?  where id = ?",
                    stuCheck.getSid(),
                    stuCheck.getCheckid(),
                    stuCheck.getStage(),
                    stuCheck.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean UpdateStageById(Long id, Integer stage) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update stucheck set stage = ?  where id = ?",
                        stage,
                        id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        boolean b = new StuCheckRepositoryImpl().UpdateStageById(2L, 1);
        System.out.println(b);
    }
}
